import {checkType, helper, lodash as _} from '@/utils';
// @ts-ignore
import iconfontData from "@/components/IconFont/icon/iconfont.json";

/**
 * 处理初始的菜单树状结构数据，格式化-抽取-标记-整合
 */
export const mapToMenuUrls = (menuList: any[]) => {

  const mapUrls = (menus: any[], urls: string[] = []) => {
    menus.forEach((menu) => {
      menu.children = menu.children ? menu.children : [];

      if (menu.children.length > 0) {
        const result = mapUrls(menu.children);
        menu.url = result.urls;
      } else {
        if(menu.url && checkType.isString(menu.url)){
          urls.push(menu.url)
        } else if(menu.url && checkType.isArray(menu.url)){
          urls = urls.concat(menu.url)
        }
      }
    });

    return {
      urls,
      menus
    }
  }

  return mapUrls(menuList).menus;
}


/**
 * 获取真实的地址
 * @param url
 * @param isHref
 * @param category
 * @returns {*}
 */
function getRealUrl(url: string, isHref: boolean, category: string) {
  if (!url || isHref) return url;
  // 拼接前缀
  if (url && checkType.isString(url)) {
    const miniUrl = url.toLocaleLowerCase();
    if (miniUrl.includes('http://') || miniUrl.includes('https://')) {
      return '';
    } else {
      url = `/${category}/${url.replace(/^\//, "")}`;
    }
  }
  return url;
}


/**
 * 获取菜单分类的label
 * @param category
 * @param EnumMenus
 * @returns {null|*}
 */
export const getMenuCategoryLabel = (category: string, EnumMenus: any[] = []) => {
  for (let i = 0; i < EnumMenus.length; i++) {
    if (category === EnumMenus[i].value) {
      return EnumMenus[i].label;
    }
  }

  return null;
};

/**
 * 获取默认登录跳转页面
 * @returns {*}
 */
export const getDefaultRedirectURL = (UrlToExtraInfoMap = {}) => {
  const urls = Object.keys(UrlToExtraInfoMap);
  return urls.length > 0 ? urls[0] : '';
};

/**
 * 获取菜单类别
 */
export const getMenuCategory = (EnumMenus: any[] = []) =>
  EnumMenus.map(val => {
    const {label, value} = val;
    let defaultUrl = '';
    try {
      defaultUrl = val.childrenMenu[0].url[0];
    } catch (e) {
      // 可能树中没有一个路由信息
    }
    return {
      label,
      value,
      url: defaultUrl,
    };
  });

/**
 * 获取菜单的类别
 * @param category
 * @param EnumMenus
 * @returns {*|{children, label}|Array|childrenMenu|Array}
 */
export const getMenusByCategory = (category: string, EnumMenus: any[] = []) => {
  for (let i = 0; i < EnumMenus.length; i++) {
    if (category === EnumMenus[i].value) {
      return EnumMenus[i].childrenMenu;
    }
  }

  return [];
};

/**
 * 获取左侧菜单
 * @param url
 * @param type
 * @param EnumMenus
 * @returns {Array|*}
 */
export const getLeftMenu = (url: string, type: string, EnumMenus: any[] = []) => {
  const menu = getMenusByCategory(type, EnumMenus);
  for (let i = 0; i < menu.length; i++) {
    if (
      (_.isArray(menu[i].url) && menu[i].url.findIndex((u: string) => u && _.isString(url) && url.includes(u)) !== -1) ||
      (_.isString(menu[i].url) && url.includes(menu[i].url))
    ) {
      return menu[i].children;
    }
  }

  return [];
};


/**
 * 检查iconfont图标是否有效
 */
export function checkIconIsExist(icon: string) {
  let isExist = false;
  try {
    const {glyphs} = iconfontData;
    if (Array.isArray(glyphs)) {
      glyphs.forEach(item => {
        if (item.font_class === icon) {
          isExist = true;
        }
      })
    }
  } catch (e) {

  }
  return isExist;
}


/**
 * 获取默认展开的菜单keys
 * @param menus
 * @param openKeys
 * @param currentUrl
 */
export const recursionOpenKeys = (menus: any[] = [], currentUrl: string, openKeys: any[] = []) => {
  for (let i = 0; i < menus.length; i++) {
    const item = menus[i];
    // 存在包含关系时
    if (Array.isArray(item.url)) {
      const urlIndex = item.url.findIndex((url: string) => url && currentUrl.includes(url),);
      if (urlIndex !== -1) {

        openKeys.push(item.url.join('-'));
      }
    }

    if (item.children && item.children.length > 0) {
      recursionOpenKeys(item.children, currentUrl, openKeys);
    }
  }
  return _.uniq(openKeys);
};

/**
 * 获取真实的菜单路由
 * @param currentUrl
 * @param url
 */
export function getMenuRealUrl(currentUrl: string, url: string[] | string) {
  if (Array.isArray(url)) {
    if (url.indexOf(currentUrl) !== -1) {
      return currentUrl;
    }
    // 存在包含关系
    const urlIndex = url.findIndex((url: string) => url && currentUrl.includes(url));
    if (urlIndex !== -1) {
      return currentUrl;
    }

    return url[0];
  }

  return currentUrl;
}
